package org.xncj.Impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xncj.OrderSetting;
import org.xncj.OrderSettingService;
import org.xncj.dao.OrderSettingDao;
import sun.java2d.pipe.SpanShapeRenderer;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Resource
    private OrderSettingDao orderSettingDao;

    @Override
    public void importPIO(List<String[]> list) {
        for (String[] s : list) {
            OrderSetting orderSetting = new OrderSetting();
            orderSetting.setOrderDate(new Date(s[0]));
            orderSetting.setNumber(Integer.parseInt(s[1]));
            orderSettingDao.importPIO(orderSetting);
        }
    }

    @Override
    public List<OrderSetting> findAllOdersetting() {
        return orderSettingDao.findAllOdersetting();
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String orderDate = sdf.format(orderSetting.getOrderDate());
        Integer number = orderSettingDao.findNumberData(orderDate);
        if (number == null){
            orderSettingDao.addNumberBydate(orderSetting);
        }else {
            HashMap<String, Object> map = new HashMap<>();
            map.put("number", orderSetting.getNumber());
            map.put("orderDate", orderDate);
            orderSettingDao.updataByNumber(map);
        }

    }

}
